/**
 * ttxs jigsaw adapter
 *
 */

const gcd = function(a, b) {
    return b ? gcd(b, a % b) : a;
};

const toNewTools = {
    parseInt(inp, ratio) {
        return parseInt(inp, ratio || 10) || 0;
    },
    parseSize(inp) {
        const arr = String(inp).split(',');

        return {
            width: this.parseInt(arr[0]),
            height: this.parseInt(arr[1])
        };
    },
    parseAspectRatio(width, height) {
        const r = gcd(width, height);

        return `${width / r}:${height / r}`;
    },
    parseFrame(inp) {
        const arr = String(inp).split(',');

        return {
            left: this.parseInt(arr[0]),
            top: this.parseInt(arr[1]),
            width: this.parseInt(arr[2]),
            height: this.parseInt(arr[3])
        };
    },
    parsePoints(points) {
        return points.map(({point: pointStr}) => {
            let tmpArr = (pointStr || '').split(',');

            return {
                x: parseFloat(tmpArr[0]) || 0,
                y: parseFloat(tmpArr[1]) || 0
            };
        });
    },
    parseUrl(url) {
        const oldBaseUrl = 'http://7xk7jb.com1.z0.glb.clouddn.com';
        const newBaseUrl = 'https://img.ttxsapp.com';

        let ret = String(url).trim();

        if(ret.indexOf(oldBaseUrl) === 0) {
            ret = ret.slice(oldBaseUrl.length);

            try {
                ret = decodeURIComponent(ret);
            }
            catch(ex) { }
        }

        if(ret.charAt(0) === '/') {
            ret = newBaseUrl + ret;
        }

        return ret;
    },

    covertLayout(layout) {
        const ret = this.parseSize(layout.size);

        // aspect ratio
        ret.aspectRatio = this.parseAspectRatio(ret.width, ret.height);

        // border width
        // old: {0 无, 1 细, 2 中, 3 粗}
        // new: {0: 无, 5: 细, 10: 中, 20 粗}
        const bwType = +layout.borderWidthType || 0;

        ret.borderWidth = bwType === 3 ? 20 : 5 * bwType;

        // border width
        // old: {0 白, 1 黑}
        const bcType = +layout.borderWidthType || 0;

        ret.borderColor = bcType ? '#000000FF' : '#FFFFFFFF';

        // effectImage
        ret.effectImage = this.parseUrl(layout.effectImage);

        // elements
        ret.elements = (layout.element || []).map(elem => {
            return this.covertElement(elem);
        });

        return ret;
    },
    covertElement(elem) {
        const type = elem.svgContent ? 'svg'
            : elem.elementPoint ? 'polygon'
            : 'rect';
        const ret = {
            type
        };

        // rect
        Object.assign(ret, this.parseFrame(elem.frame));

        // svg
        if(type === 'svg') {
            ret.svgContent = String(elem.svgContent).trim();
        }
        else if(type === 'polygon') {
            ret.points = this.parsePoints(elem.elementPoint);
        }

        return ret;
    }
};

module.exports = {
    covertToNew(data) {
        let layouts = data.otherSize || [];

        if(!Array.isArray(layouts)) {
            layouts = [layouts];
        }

        layouts.unshift({
            size: data.size,
            sizeType: data.sizeType,
            borderWidthType: data.borderWidthType,
            borderColorType: data.borderColorType,
            effectImage: data.effectImage,
            element: data.element
        });

        return {
            version: data.version || '0.0.1',
            slotCount: toNewTools.parseInt(data.num),
            layouts: layouts.map(layout => {
                return toNewTools.covertLayout(layout);
            })
        };
    }
};
